package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.LabelDTO;
import com.poetize.plus.entity.dto.page.LabelPageDTO;
import com.poetize.plus.entity.vo.LabelVO;
import com.poetize.plus.entity.vo.SelectVO;
import com.poetize.plus.entity.vo.page.LabelPageVO;
import com.poetize.plus.service.LabelService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zuosy
 * @Date 2024/3/30 14:39
 **/
@Tag(name = "标签信息")
@RestController
@RequestMapping("/admin/label")
@RequiredArgsConstructor
public class LabelController {

    private final LabelService labelService;

    @GetMapping("/labelPage")
    @Operation(summary = "标签分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<LabelPageVO>> labelPage(@Validated LabelPageDTO labelPageDTO){
        return R.ok(labelService.labelPage(labelPageDTO));
    }

    @PostMapping("/addLabel")
    @Operation(summary = "新增标签信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addLabel(@RequestBody @Validated(Insert.class) LabelDTO labelDTO){
        return R.ok(labelService.addLabel(labelDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "标签信息详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<LabelVO> queryLabel(@PathVariable("id") Long id){
        return R.ok(labelService.queryLabel(id));
    }

    @PostMapping("/updateLabel")
    @Operation(summary = "修改标签信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateLabel(@RequestBody @Validated(Update.class) LabelDTO labelDTO){
        return R.ok(labelService.updateLabel(labelDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除标签信息")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> deleteSort(@PathVariable("id") Long id){
        return R.ok(labelService.deleteSort(id));
    }

    @GetMapping("/labelSelect")
    @Operation(summary = "标签下拉框")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<List<SelectVO>> labelSelect(@RequestParam(value = "sortId",required = false) Long sortId){
        return R.ok(labelService.labelSelect(sortId));
    }
}
